﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GPSTrackingWebsiteManager.GPSTrackingController;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Pages
{
    public partial class ClientsRouteHistory : ClassManager.SessionManagerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static ArrayOfString wsLoadRouteHistory(string imei, DateTime date, bool isFirstSelection)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsLoadRouteHistory(imei, date, isFirstSelection);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /*[System.Web.Services.WebMethod]
        public static string wsGetResumenRouteHistory(int numControl)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsGetResumenRouteHistory(numControl);
            }
            catch (Exception e)
            {
                throw e;
            }
        }*/

        protected void ClientsProcessGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            //the list item represents the column(Text: NameColumn, Value: Type, enabled: isPrimaryKey)
            List<ListItem> list = new List<ListItem>();
            list.Add(new ListItem("Username", "System.String", false));
            list.Add(new ListItem("Name", "System.String", false));
            list.Add(new ListItem("GPSImei", "System.String", false));
            list.Add(new ListItem("GPSName", "System.String", false));
            list.Add(new ListItem("Date", "System.String", false));
            list.Add(new ListItem("TimeBegin", "System.String", false));
            list.Add(new ListItem("TimeEnd", "System.String", false));
            list.Add(new ListItem("Status", "System.String", false));
            list.Add(new ListItem("Process", "System.String", false));
            list.Add(new ListItem("LastExecution", "System.String", false));
            list.Add(new ListItem("ClientGroupID", "System.Int64", false));
            list.Add(new ListItem("ClientGroupName", "System.String", false));
            list.Add(new ListItem("LatOrigin", "System.String", false));
            list.Add(new ListItem("LngOrigin", "System.String", false));
            ClientsProcessGrid.DataSource = getDataSet(list);
        }

        [System.Web.Services.WebMethod]
        public static string wsGetCompaniesByAdminUsername()
        {
            try
            {
                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsGetCompaniesByAdminUsername(objSessionManager.CompanyID, HttpContext.Current.User.Identity.Name, objSessionManager.IsMaster);
            }

            catch (Exception e)
            {
                throw e;
            }
        }

        [System.Web.Services.WebMethod]
        public static string wsGetClientsProcessByDateByCompanyID(DateTime dateBegin, int companyID)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsGetClientsProcessByDatesByCompanyID(dateBegin, companyID);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [System.Web.Services.WebMethod]
        public static string wsGetAllBusRouteDetailsByBusRouteID(int busRouteID)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsGetAllBusRouteDetailsByBusRouteID(busRouteID);
            }

            catch (Exception e)
            {
                throw e;
            }
        }

        [System.Web.Services.WebMethod]
        public static ArrayOfString wsLoadClientsRouteHistory(int companyID, string username, string gpsImei, DateTime date, bool isFirstSelection)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsLoadClientsRouteHistory(companyID, username, gpsImei, date, isFirstSelection);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [System.Web.Services.WebMethod]
        public static string wsGetClientsToVisitByUsernameByGPSImeiByDate(int companyID, string username, string gpsImei, DateTime date)
        {
            try
            {
                return new GPSTrackingController.GPSTrackingControllerSoapClient().wsGetClientsToVisitByUsernameByGPSImeiByDate(companyID, username, gpsImei, date);
            }

            catch (Exception e)
            {
                throw e;
            }
        }

        protected void rgCompaniesGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            //the list item represents the column(Text: NameColumn, Value: Type, enabled: isPrimaryKey)
            List<ListItem> list = new List<ListItem>();
            list.Add(new ListItem("CompanyID", "System.Int64", true));
            list.Add(new ListItem("Name", "System.String", false));
            rgCompaniesGrid.DataSource = getDataSet(list);
        }

        protected DataSet getDataSet(List<ListItem> list)
        {
            // Create a new DataTable.
            System.Data.DataTable table = new DataTable("ParentTable");

            // Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.  
            foreach (ListItem item in list)
            {
                column = new DataColumn();
                column.DataType = System.Type.GetType(item.Value);
                column.ColumnName = item.Text;
                column.ReadOnly = true;
                column.Unique = item.Enabled;
                // Add the Column to the DataColumnCollection.
                table.Columns.Add(column);

                if (item.Enabled)
                {
                    // Make the ID column the primary key column.
                    DataColumn[] PrimaryKeyColumns = new DataColumn[1];
                    PrimaryKeyColumns[0] = table.Columns[item.Text];
                    table.PrimaryKey = PrimaryKeyColumns;
                }
            }

            // Instantiate the DataSet variable.
            DataSet dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);

            return dataSet;
        }
    }
}